//
//  MSRegisterController.m
//  觅尚
//
//  Created by No.8 on 2019/3/19.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSRegisterController.h"
#import "MSRegisterView.h"
#import "MSPresentperfectController.h"
#import "MSCompanyController.h"
#import "MSAgreementController.h"



@interface MSRegisterController ()

/** h界面view*/
@property(nonatomic,strong)MSRegisterView *RegisterView;


/** 验证码id*/
@property(nonatomic,strong)NSString *msgId;

@end

@implementation MSRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self AddRegisterView];
    
    [self AnimationClick];
}

- (void)AddRegisterView{
    
    self.RegisterView = [[MSRegisterView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.RegisterView];
    
   
    
    self.RegisterView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"登录bg"]];
    UIView * ImageView = [[UIView alloc] initWithFrame:self.RegisterView.frame];
    UIImage * image = [UIImage imageNamed:@"登录bg"];
    ImageView.layer.contents = (__bridge id)image.CGImage;
    /** 设置对齐模式*/
    ImageView.layer.contentsGravity = kCAGravityResize;
    [self.RegisterView addSubview:ImageView];
    
     [self.RegisterView addSubview:self.RegisterView.registertitle];
    
    [self.RegisterView addSubview:self.RegisterView.iphoneTextField];
    
    [self.RegisterView addSubview:self.RegisterView.codeButton];
    
    [self.RegisterView addSubview:self.RegisterView.iphonelineIMG];
    
    [self.RegisterView addSubview:self.RegisterView.codeTextField];
    
    [self.RegisterView addSubview:self.RegisterView.codelineIMG];
    
    [self.RegisterView addSubview:self.RegisterView.passworldTextField];
    
    [self.RegisterView addSubview:self.RegisterView.passworldlineIMG];
    
    [self.RegisterView addSubview:self.RegisterView.passworldButton];
    
    
    [self.RegisterView addSubview:self.RegisterView.registerTextField];
    
    [self.RegisterView addSubview:self.RegisterView.registerlineIMG];
    
    [self.RegisterView addSubview:self.RegisterView.registerButton];
    
    [self.RegisterView addSubview:self.RegisterView.showTitle];
    
    [self.RegisterView addSubview:self.RegisterView.userdelButton];
    
    [self.RegisterView addSubview:self.RegisterView.HeTitle];
    
    [self.RegisterView addSubview:self.RegisterView.PrivacyButton];
    
    [self.RegisterView addSubview:self.RegisterView.LoginButton];
    
    
}
//添加事件
- (void)AnimationClick{
    
    // 注册按钮
    [self.RegisterView.registerButton addTarget:self action:@selector(AnimationregisterButtonlick) forControlEvents:UIControlEventTouchUpInside];
    
    //获取验证码按钮
    [self.RegisterView.codeButton addTarget:self action:@selector(AnimationcodeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    //密码可见按钮
    [self.RegisterView.passworldButton addTarget:self action:@selector(AnimationpassworldButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //登录按钮
    [self.RegisterView.LoginButton addTarget:self action:@selector(AnimationLoginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.RegisterView.userdelButton addTarget:self action:@selector(userdelButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.RegisterView.PrivacyButton addTarget:self action:@selector(PrivacyButtonClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)userdelButtonClick{
    MSAgreementController * AgreementVC = [[MSAgreementController alloc] init];
    AgreementVC.URLstr = MSagreement_HTML;
    [self presentViewController:AgreementVC animated:NO completion:nil];
    
}

- (void)PrivacyButtonClick{
    MSAgreementController * AgreementVC = [[MSAgreementController alloc] init];
    AgreementVC.URLstr = MSpolicy_HTML;
    [self presentViewController:AgreementVC animated:NO completion:nil];
    
}
// 注册按钮
- (void)AnimationregisterButtonlick{
    
//    MSCompanyController * com = [[MSCompanyController alloc] init];
//    [self presentViewController:com animated:NO completion:nil];
    NSString *iphoneTextStr = [self.RegisterView.iphoneTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *CodeTextStr = [self.RegisterView.codeTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *PassTextStr = [self.RegisterView.passworldTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *RegisterTextStr = [self.RegisterView.registerTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (iphoneTextStr.length != 11) {
        [MBManager showBriefAlert:@"手机号不正确"];
        return;
    }
    if (CodeTextStr.length == 0 ) {

        [MBManager showBriefAlert:@"验证码不能为空"];
        return;
    }
    if (CodeTextStr.length != 6) {
        
        [MBManager showBriefAlert:@"验证码不正确"];
        return;
    }
    if (PassTextStr.length == 0) {

        [MBManager showBriefAlert:@"密码不能为空"];
        return;
    }
    if (RegisterTextStr.length == 0) {
        
        [MBManager showBriefAlert:@"注册码不能为空"];
        return;
    }
    NSDictionary * params = @{@"businessCode":RegisterTextStr,@"code":CodeTextStr,@"msgId":self.msgId};
    [HTTPRequest POST:MSgetCodeBussiness params:params succeed:^(id responseObject) {
        if (CodeStr) {
            MSCompanyController * CompanyVC = [[MSCompanyController alloc] init];
            CompanyVC.belongBusid = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"busid"]];
            CompanyVC.belongName = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"businessName"]];
            CompanyVC.msgId = self.msgId;
            CompanyVC.CodeTextStr = CodeTextStr;
            CompanyVC.registrationCode = RegisterTextStr;
            CompanyVC.iphoneTextStr = iphoneTextStr;
            CompanyVC.PassTextStr = PassTextStr;
            [self presentViewController:CompanyVC animated:NO completion:nil];
        }else{
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        [MBManager showBriefAlert:Failed];
    }];


    
}

//获取验证码按钮
- (void)AnimationcodeButtonClick{
    NSString *iphoneTextStr = [self.RegisterView.iphoneTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (iphoneTextStr.length != 11) {
        [MBManager showBriefAlert:@"手机号不正确!"];
        return;
    }else{
        
        [self timeCountDown];
        
        [self CodepostUrl];
    }
}
- (void)timeCountDown {
    __block int timeout = 59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout <= 0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                self.RegisterView.codeButton.userInteractionEnabled = YES;
                self.RegisterView.codeButton.titleLabel.font = [UIFont systemFontOfSize:Fontfour];
                [self.RegisterView.codeButton setTitle:@"重新接收" forState:UIControlStateNormal];
            });
        }else{
            int seconds = timeout % 90;
            NSString *strTime = [NSString stringWithFormat:@"%d秒后重发", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.RegisterView.codeButton.userInteractionEnabled = NO;
                self.RegisterView.codeButton.titleLabel.font = [UIFont systemFontOfSize:Fontfour];
                [self.RegisterView.codeButton setTitle:strTime forState:UIControlStateNormal];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}


- (void)CodepostUrl{
    NSString *iphoneTextStr = [self.RegisterView.iphoneTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSDictionary * params = @{@"phone":iphoneTextStr};
    [HTTPRequest POST:MSsendSms params:params succeed:^(id responseObject) {
        if (CodeStr) {
            self.msgId = responseObject[@"data"][@"msgId"];
            [MBManager showBriefAlert:@"发送成功"];
        }else{
            
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
        
    } failure:^(NSError *error) {
        [MBManager showBriefAlert:Failed];
    }];
    
}
//密码可见按钮
- (void)AnimationpassworldButtonClick:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.RegisterView.passworldTextField.secureTextEntry = NO;
    }else{
        self.RegisterView.passworldTextField.secureTextEntry = YES;
    }
}

//登录按钮
- (void)AnimationLoginButtonClick{
    
    [self dismissViewControllerAnimated:NO completion:nil];
}
@end
